import {
	getCurrentUser
} from "@/network/sys/user";
import {
	logout
} from "@/network/login.js";
import {
	ElMessage
} from 'element-plus';
const DUser = {
	state: {
		id: '',
		name: '',
		realName: '',
		superAdmin: ''
	},
	mutations: {
		SET_ID: (state, value) => {
			state.id = value;
		},
		SET_NAME: (state, value) => {
			state.name = value;
		},
		SET_SUPERADMIN: (state, value) => {
			state.superAdmin = value;
		},
		SET_REALNAME: (state, value) => {
			state.realName = value;
		}
	},
	actions: {
		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				getCurrentUser().then(res => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					const userTmp = res.data;
					commit('SET_ID', userTmp.id);
					commit('SET_NAME', userTmp.username);
					commit('SET_REALNAME', userTmp.realName);
					commit('SET_SUPERADMIN', userTmp.superAdmin);

					resolve(res)
				}).catch(error => {
					reject(error)
				})
			})
		},
		LogOut({dispatch,commit}) {
			return new Promise((resolve, reject) => {
				logout().then(() => {
					//清空用户信息
					commit('SET_ID', '');
					commit('SET_NAME', '');
					commit('SET_REALNAME', '');
					commit('SET_SUPERADMIN', '');
					//清空tabs
					dispatch('deleteAllConntenttabs' );
					//清空权限
					dispatch('DeletePermissions' );
					
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		}

	}
}

export default DUser
